/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.daoImpl;

import br.com.deployment.dao.CategoryDAO;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.genericdao.GenericDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author ely
 */
@Stateless
@LocalBean
public class CategoryDAOImpl extends GenericDAO<Category> implements CategoryDAO {

    public CategoryDAOImpl() {
        super(Category.class);
    }

    @Override
    public List<Category> findCategory(Category category) {

        List<Category> lstCategory = new ArrayList<Category>();
        String sql = "SELECT c FROM Category c ";

        if (category.getName() != null && !category.getName().equals("")) {
            sql += " WHERE LOWER(c.name) LIKE LOWER(:name) ";
        }

        Query q = getEntityManager().createQuery(sql);

        if (category.getName() != null && !category.getName().equals("")) {
            q.setParameter("name", "%" + category.getName() + "%");
        }
        lstCategory = q.getResultList();
        return lstCategory;
    }

    @Override
    public Category findListCategory(Category category) {
        String sql = "SELECT c FROM Questionnaire q Join q.categorys c WHERE"
                + " c.id = :id";
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", category.getId());
        return (Category) query.getSingleResult();
    }

    @Override
    public List<Category> findCategoryQuestionnaire(Questionnaire q) {
        String sql = "SELECT c FROM Questionnaire q JOIN q.categorys c WHERE"
                + " q.id = :id ORDER BY c.cod ASC";
        
        Query query = getEntityManager().createQuery(sql);
        query.setParameter("id", q.getId());
        return (List<Category>) query.getResultList();
    }

    @Override
    public List<Category> findCategoryComplete(Category category) {
        List<Category> lista = new ArrayList<Category>();
        String sql = "Select c from Category c where lower(c.name) like lower(:name)";
        Query query = getEntityManager().createQuery(sql);

        if (category.getName() != null) {
            query.setParameter("name", "%" + category.getName() + "%");
        }

        lista = query.getResultList();
        return lista;
    }
}
